package com.dferreira.languagesteach.data_parsers;

import com.dferreira.languagesteach.data_models.Exercise;
import com.dferreira.languagesteach.data_models.Section;
import com.dferreira.languagesteach.data_models.Workout;

import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

import java.util.ArrayList;

public class XMLWorkoutParser extends DefaultHandler {
    private final String SECTION_TAG = "section";

    private final ArrayList<Section> sections;
    private ArrayList<Exercise> sectionExercises;

    private final Workout workout;

    /**
     * Starts the variables to the workout parsing
     */
    public XMLWorkoutParser() {
        super();
        workout = new Workout();
        sections = new ArrayList<Section>();
    }

    /**
     * Called when tag starts
     *
     * @param uri        The Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.
     * @param localName  The local name (without prefix), or the empty string if Namespace processing is not being performed.
     * @param qName      The qualified name (with prefix), or the empty string if qualified names are not available.
     * @param attributes The attributes attached to the element. If there are no attributes, it shall be an empty Attributes object.
     */
    @Override
    public void startElement(String uri, String localName, String qName,
                             Attributes attributes) {
        String EXERCISE_TAG = "exercise";
        String PREFIX_ATTRIBUTE = "prefix";
        String TITLE_ATTRIBUTE = "title";
        String CLASS_ATTRIBUTE = "class";

        if (localName.equals(SECTION_TAG)) {
            Section section;
            section = new Section();
            section.setPrefix(attributes.getValue(PREFIX_ATTRIBUTE));
            sections.add(section);
            sectionExercises = new ArrayList<Exercise>();
        } else {
            if (localName.equals(EXERCISE_TAG)) {
                Exercise exercise;
                exercise = new Exercise();
                exercise.setTitle(attributes.getValue(TITLE_ATTRIBUTE));
                exercise.set_class(attributes.getValue(CLASS_ATTRIBUTE));
                sectionExercises.add(exercise);
            }
        }
    }

    /**
     * Called when tag ends
     *
     * @param uri       The Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.
     * @param localName The local name (without prefix), or the empty string if Namespace processing is not being performed.
     * @param qName     The qualified name (with prefix), or the empty string if qualified names are not available.
     */
    @Override
    public void endElement(String uri, String localName, String qName) {
        String WORKOUT_SCREEN_TAG = "workout_screen";

        if (localName.equals(WORKOUT_SCREEN_TAG)) {
            workout.setSections(sections);
        } else {
            if (localName.equals(SECTION_TAG)) {
                Section section;
                section = sections.get(sections.size() - 1);
                section.setExercisesLst(sectionExercises);
            }
        }
    }

    /**
     * @return workout full of sections and this one full of exercises
     */
    public Workout getWorkout() {
        return workout;
    }

}
